#include "dtwentymodel.h"
#include "dtwentyitem.h"
#include "campaign.h"



DTwentyModel::DTwentyModel(QObject *parent) :
    ItemTreeModel(parent)
{
    qRegisterMetaType<DTwentyItem*>();
}

QVariant DTwentyModel::data(const QModelIndex &index, int role) const
{
    if (role < Role_FIRST)
    {
        return ItemTreeModel::data(index, role);
    }

    DTwentyItem* item = qobject_cast<DTwentyItem*>(itemFromIndex(index));
    if (!index.isValid())
    {
        item = qobject_cast<DTwentyItem*>(root());
    }

    if (!item)
    {
        return ItemTreeModel::data(index, role);
    }

    switch (role)
    {
    case Role_CHILD_OPTIONS:
    {
        Campaign* campaign = qobject_cast<Campaign*>(item);
        if (!campaign)
        {
            return QVariant();
        }

        QVariantMap ret;
        foreach (int typeId, campaign->childTypes())
        {
            const QMetaObject* mo = QMetaType::metaObjectForType(typeId);
            if (!mo)
            {
                continue;
            }

            QString className = mo->className();
            if (className.isEmpty())
            {
                className = "unknown";
            }

            ret.insert(className, typeId);
        }
        return ret;

    }
    default:
        break;
    }

    return QVariant();
}
